import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/gateway/api.dart";
import "package:ct_pa_majordomo/pages/device/gateway/bound_locks/index.dart";
import "package:ct_pa_majordomo/pages/device/gateway/detail/section/edit_gateway_name.dart";
import "package:ct_pa_majordomo/pages/device/gateway/index.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/select_group.dart";
import "package:ct_pa_majordomo/widgets/tai_dialog/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Detail extends StatefulWidget {
  final Map gateway;
  const Detail({super.key, required this.gateway});

  @override
  State<StatefulWidget> createState() => _DetailState();

}

class _DetailState extends State<Detail> {
  Map _detail = {};
  List _locks = [];

  void _getGatewayInfo() async {
    Map resData = await plugApi.getGatewayInfo(plugId: widget.gateway["plugId"]);
    setState(() {
      _detail = resData["data"];
    });
  }

  void _removeGateway() async {
    taiDialog(
      context: context,
      child: Text("确定删除网关？", textAlign: TextAlign.center,),
      onConfirm: (taiContext) async {
        EasyLoading.show();
        await plugApi.removeGateway(plugId: widget.gateway["plugId"]);
        EasyLoading.showToast("删除成功");
        Stacker.pushNamedAndRemoveUntil(Keys.DEVICE_GATEWAY, ModalRoute.withName(Keys.MAIN));
        // if (taiContext.mounted) {
        //   Navigator.of(taiContext).pop();
        // }
        // await Future.delayed(const Duration(seconds: 1));
        // if (context.mounted) {
        //   // Stacker.pushNamedAndRemoveUntil(Keys.DEVICE_GATEWAY, ModalRoute.withName(Keys.DEVICE_GATEWAY));
        //   Stacker.popUntil(ModalRoute.withName(Keys.DEVICE_GATEWAY));
        //   // Navigator.popUntil(context, ModalRoute. withName("/gateway"));
        // }
      }
    );

  }

  void _getAssociatedLockList() async {
    Map resData = await plugApi.getAssociatedLockList(plugId: widget.gateway["plugId"]);
    setState(() {
      _locks = resData["data"] ?? [];
    });
  }

  @override
  void initState() {
    _getGatewayInfo();
    _getAssociatedLockList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: Text(widget.gateway["plugName"], style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimary,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        children: [
          MenuBlock(
            borderRadius: BorderRadius.circular(0),
            items: [
              MenuBlockItem(
                text: "网关名称",
                showArrow: true,
                suffix: Text(_detail["plugName"] ?? ""),
                onTap: () {
                  editGatewayName(
                    context: context,
                    value: _detail["plugName"],
                    onChange: (value) => setState(() {
                      _detail["plugName"] = value;
                    }),
                  );
                },
              ),
              MenuBlockItem(
                text: "状态",
                showArrow: false,
                suffix: Text(widget.gateway["isOnline"] == "1" ? "在线" : "离线"),
              ),
              MenuBlockItem(
                text: "WiFi",
                showArrow: false,
                suffix: const Text("泰寓1776544585"),
              ),
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            borderRadius: BorderRadius.circular(0),
            items: [
              MenuBlockItem(
                text: "绑定的智能锁",
                showArrow: true,
                suffix: Text(_locks.length.toString()),
                onTap: () {
                  Stacker.pushNamed(Keys.DEVICE_GATEWAY_BOUND_LOCKS, arguments: widget.gateway["plugId"]);
                },
              ),
            ],
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(ThemeVars.paddingPage, 60, ThemeVars.paddingPage, 0),
            child: CupertinoButton(
              color: ThemeVars.colorError,
              onPressed: () {
                _removeGateway();
              },
              child: const Text("删除", style: TextStyle(color: Colors.white),),
            ),
          ),
        ],
      ),
    );
  }

}